import React from "react"
import { Badge } from 'antd'
import {messages} from "share/common";
import menuRoute from 'routes/menuRoute'
import ResizeTable from 'components/resize-table'
import ReportService from "containers/report-list/report-list.service";

class ReportList extends React.Component{
  constructor(){
    super();
    this.state = {
      loading: false,
      data: [],
      pagination: {
        total: 0,
        current: 1,
      },
      page: 0,
      pageSize: 10,
      columns: [
        {title: messages("common.sequence"), dataIndex: 'index', width: '6%'},    //序号
        {title: messages('report-list.key1'/*报表名称*/), dataIndex: "copyName"},    //报表名称
        {title: messages('report-list.key2'/*报表描述*/), dataIndex: "copyComments"},    //报表描述
        {title: messages("common.column.status"), dataIndex: "status",width: '12%', render: status => ( //状态
            <Badge  status = {status === "open" ? 'success' : 'error'}
                    text = {this.refreshStatus(status)}/>
          )},
        {title: messages("common.operation"), key: 'operation', width: '12%', render: (text, record) => (  //操作
            <span>
            <a onClick={(e) => this.handleViewDetail(e, record)}>{messages ("common.detail")}</a>
          </span>)
        }   //操作
      ],
      reportLineOID: ""
    };
    this.reportDetail = menuRoute.getRouteItem('report-list-details', 'key');
  }
  componentWillMount() {
    this.getList();
  }

  //列表状态国际化传值
  refreshStatus = (status) => {
    if(status === "open")
      return messages('report-list.key3'/*启用*/)
    if(status === "undistributed")
      return messages('report-list.key4'/*未分配*/)
    else
      return messages('report-list.key5'/*未启用*/)

  }

  getList = () => {
    this.setState({loading: true});
    ReportService.getVisibleReport().then((response) => {
      response.data.map((item,index) => {
        item.index = index + 1;
        item.key = item.reportLineOID;
      });
      this.setState({
        data: response.data,
        loading: false,
        pagination: {
          total: Number(response.headers['x-total-count']) ? Number(response.headers['x-total-count']) : 0,
          onChange: this.onChangePager,
          current: this.state.page + 1
        }
      })
    });
  }

  //翻页
  onChangePager = (page) => {
    if(page - 1 !== this.state.page)
      this.setState({page: page - 1}, this.getList);
  };

  //查看报表详情
  handleViewDetail = (e, record) => {
    let path = this.reportDetail.url.replace(":reportLineOID", record.reportLineOID);
    this.setState({reportLineOID: record.reportLineOID});
    this.context.router.push(path);
  };

  render(){
    const { data, pagination, columns, loading } = this.state;
    return(
      <div>
        <ResizeTable
          columns={columns}
          dataSource={data}
          pagination={pagination}
          loading={loading}
          bordered
        />
      </div>
    )
  }
}
ReportList.contextTypes = {
  router: React.PropTypes.object
};

export default ReportList;
